import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.*;

public class test2 {
    private JPanel jp;
    private JTextField textField1;
    private JTextField textField2;
    private JButton registerbtn;
    private JButton backbtn;
    JFrame jf = new JFrame("音乐播放器 - 注册界面");


    public test2() {
        setupUI();
        addActionListeners();

    }

    private void setupUI() {
        // UI组件的初始化和布局设置...
        jf.setSize(500, 500);
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        initComponents();
        backbtn.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                test1 t1 = new test1();
                jf.setVisible(false);
            }
        });

        jf.add(jp);
        jf.setVisible(true);
    }
    private void initComponents() {
        jp = new JPanel();
        textField1 = new JTextField(20);
        textField2 = new JPasswordField(20);
        registerbtn = new JButton("注册");
        backbtn = new JButton("返回");
        jp.setLayout(null); // 设置面板布局为网格布局3
        JLabel j1=new JLabel("用户名:");
        JLabel j2=new JLabel("密码:");
        j1.setBounds(120,100,100,30);
        j2.setBounds(120,150,100,30);
        textField1.setBounds(180,100,200,30);
        textField2.setBounds(180,150,200,30);
        backbtn.setBounds(180,300,200,30);
        registerbtn.setBounds(180,250,200,30);

        jp.add(j1);
        jp.add(textField1);
        jp.add(j2);
        jp.add(textField2);
        jp.add(backbtn);
        jp.add(registerbtn);
    }

    private void addActionListeners() {
        registerbtn.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                String username = textField1.getText();
                String password = textField2.getText();

                // 这里添加数据库注册逻辑
                if (isUsernameUnique(username)) {
                    if (registerUser(username, password)) {
                        JOptionPane.showMessageDialog(jf, "注册成功", "注册信息", JOptionPane.INFORMATION_MESSAGE);
                        // 注册成功后清空文本框
                        textField1.setText(""); // 清空用户名文本框
                        textField2.setText(""); // 清空密码文本框
                        // 可选：将光标移动到用户名文本框，方便用户操作
                        textField1.requestFocus();
                    } else {
                        JOptionPane.showMessageDialog(jf, "注册失败，请稍后再试。", "错误信息", JOptionPane.ERROR_MESSAGE);
                    }
                } else {
                    JOptionPane.showMessageDialog(jf, "用户名已存在", "错误信息", JOptionPane.ERROR_MESSAGE);
                }
            }
        });
    }

    private boolean isUsernameUnique(String username) {
        // 编写SQL查询语句，检查用户名是否已存在
        String querySQL = "SELECT COUNT(*) FROM users WHERE username = ?";

        try (Connection conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/mysql", "root", "190825DLdl"); // 替换为实际的数据库URL、用户名和密码
             PreparedStatement pstmt = conn.prepareStatement(querySQL)) {

            pstmt.setString(1, username);

            // 执行查询
            ResultSet resultSet = pstmt.executeQuery();

            // 处理查询结果
            if (resultSet.next()) {
                // 如果计数大于0，表示用户名已存在
                int count = resultSet.getInt(1);
                return count == 0;
            }
        } catch (SQLException e) {
            // 异常处理，打印错误信息
            System.out.println("数据库查询错误: " + e.getMessage());
        }

        // 如果发生异常或查询失败，返回false
        return false;
    }


    // 注册用户
    private boolean registerUser(String username, String password) {
        String insertSQL = "INSERT INTO users (username, password) VALUES (?, ?)";
        try (Connection conn = DriverManager.getConnection(
                "jdbc:mysql://127.0.0.1:3306/mysql", "root", "190825DLdl"); // 替换为实际的数据库URL和登录凭证
             PreparedStatement pstmt = conn.prepareStatement(insertSQL)) {

            pstmt.setString(1, username);
            pstmt.setString(2, password); // 实际应用中密码应该加密存储

            int rowsAffected = pstmt.executeUpdate();
            return rowsAffected > 0;
        } catch (SQLException e) {
            System.out.println("数据库错误: " + e.getMessage());
            return false;
        }
    }

    public static void main(String[] args) {
        new test2();

    }
}